package cmsapi

import (
	// "net/http"
	"encoding/json"
	"strconv"
	// "time"
	// "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"
	// "github.com/jinzhu/gorm"
	
	"oxy_server/module/log"
	"oxy_server/model"
	"oxy_server/until"
)

type (
	bom struct {
		ID bson.ObjectId `json:"_id" bson:"_id"`
		ProductName 					string `json:"productname" bson:"productname"`
		ProductPic 						string `json:"productpic" bson:"productpic"`
		PackagetPic 					string `json:"packagepic" bson:"packagepic"`
		Cost		 					string `json:"cost" bson:"cost"`
		Fabric		 					string `json:"fabric" bson:"fabric"`
		Material 						string `json:"material" bson:"material"`
		BileCloth 						string `json:"bilecloth" bson:"bilecloth"`
		MatchingFlannelette 			string `json:"matchingflannelette" bson:"matchingflannelette"`
		SupplementaryMaterial 			string `json:"supplementarymaterial" bson:"supplementarymaterial"`
		FillerOne						string `json:"fillerone" bson:"fillerone"`
		FillerTwo						string `json:"fillertwo" bson:"fillertwo"`
		RawMaterialCost 				string `json:"rawmaterialcost" bson:"rawmaterialcost"`
		MachiningTubePinCost 			string `json:"machiningtubepincost" bson:"machiningtubepincost"`
		PackageCost						string `json:"packagecost" bson:"packagecost"`
		ExpressCost						string `json:"expresscost" bson:"expresscost"`
		IncludeForwardExpressCost		string `json:"includeforwardexpresscost" bson:"includeforwardexpresscost"`
		ExchangeRate					string `json:"exchangerate" bson:"exchangerate"`
		ExchangeExpressCost				string `json:"exchangeexpresscost" bson:"exchangeexpresscost"`
		IncludeExchangeExpressCost		string `json:"includeexchangeexpresscost" bson:"includeexchangeexpresscost"`
		ExchangeLoss					string `json:"exchangeloss" bson:"exchangeloss"`
		IncludeExchangeLossFactoryCost	string `json:"includeexchangelossfactorycost" bson:"includeexchangelossfactorycost"`
		PlatformBuckle					string `json:"platformbuckle" bson:"platformbuckle"`
		TaxPoint						string `json:"taxpoint" bson:"taxpoint"`
		MarketingCosts					string `json:"marketingcosts" bson:"marketingcosts"`
		ProfitMargin					string `json:"profitmargin" bson:"profitmargin"`
		Quotation						string `json:"quotation" bson:"quotation"`
		RequiredPrice					string `json:"requiredprice" bson:"requiredprice"`
		MarkupRate						string `json:"markuprate" bson:"markuprate"`
		CustomizedPrice					string `json:"customizedprice" bson:"customizedprice"`
	}
	insertbom struct {
		ProductName 					string `json:"productname" bson:"productname"`
		ProductPic 						string `json:"productpic" bson:"productpic"`
		PackagetPic 					string `json:"packagepic" bson:"packagepic"`
		Cost		 					string `json:"cost" bson:"cost"`
		Fabric		 					string `json:"fabric" bson:"fabric"`
		Material 						string `json:"material" bson:"material"`
		BileCloth 						string `json:"bilecloth" bson:"bilecloth"`
		MatchingFlannelette 			string `json:"matchingflannelette" bson:"matchingflannelette"`
		SupplementaryMaterial 			string `json:"supplementarymaterial" bson:"supplementarymaterial"`
		FillerOne						string `json:"fillerone" bson:"fillerone"`
		FillerTwo						string `json:"fillertwo" bson:"fillertwo"`
		RawMaterialCost 				string `json:"rawmaterialcost" bson:"rawmaterialcost"`
		MachiningTubePinCost 			string `json:"machiningtubepincost" bson:"machiningtubepincost"`
		PackageCost						string `json:"packagecost" bson:"packagecost"`
		ExpressCost						string `json:"expresscost" bson:"expresscost"`
		IncludeForwardExpressCost		string `json:"includeforwardexpresscost" bson:"includeforwardexpresscost"`
		ExchangeRate					string `json:"exchangerate" bson:"exchangerate"`
		ExchangeExpressCost				string `json:"exchangeexpresscost" bson:"exchangeexpresscost"`
		IncludeExchangeExpressCost		string `json:"includeexchangeexpresscost" bson:"includeexchangeexpresscost"`
		ExchangeLoss					string `json:"exchangeloss" bson:"exchangeloss"`
		IncludeExchangeLossFactoryCost	string `json:"includeexchangelossfactorycost" bson:"includeexchangelossfactorycost"`
		PlatformBuckle					string `json:"platformbuckle" bson:"platformbuckle"`
		TaxPoint						string `json:"taxpoint" bson:"taxpoint"`
		MarketingCosts					string `json:"marketingcosts" bson:"marketingcosts"`
		ProfitMargin					string `json:"profitmargin" bson:"profitmargin"`
		Quotation						string `json:"quotation" bson:"quotation"`
		RequiredPrice					string `json:"requiredprice" bson:"requiredprice"`
		MarkupRate						string `json:"markuprate" bson:"markuprate"`
		CustomizedPrice					string `json:"customizedprice" bson:"customizedprice"`
	}
)

func GetBomListHandler(c echo.Context) error {
	currentpage,err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage,err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	model.DB().Dbname="OxyCms"
	model.DB().Document="bom"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	query,count,err:=model.DB().Query(M, currentpage, perpage)
	if err != nil {
		log.Error("查询出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	ps:=[]bom{}
	query.All(&ps)
	str, err := json.Marshal(ps)
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":string(str),
		"count":count,
	})

	return nil
}

func AddBomHandler(c echo.Context) error {
	b:= &insertbom{}
	if err := c.Bind(b); err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	model.DB().Dbname="OxyCms"
	model.DB().Document="bom"
	err:=model.DB().Insert(b)
	if err != nil {
		log.Error("插入出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
	})

	return nil	
}
func UpdateBomHandler(c echo.Context) error {
	id := c.QueryParam("id") 
	log.Info("UpdateBomHandler",id)
	b:= &insertbom{}
	if err := c.Bind(b); err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	model.DB().Dbname="OxyCms"
	model.DB().Document="bom"
	err:= model.DB().Update(bson.M{"_id":bson.ObjectIdHex(id)} , b)
	if err != nil{
		log.Error("更新出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
	})

	return nil
}
func GetBomInfoHandler(c echo.Context) error {
	log.Info("GetProductInfoHandler")
	id := c.QueryParam("id")
	model.DB().Dbname="OxyCms"
	model.DB().Document="products"
	query,err := model.DB().QueryOne(bson.M{"_id":bson.ObjectIdHex(id)})
	if err != nil{
		log.Error("products查询出错",err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	p:= product{}
	query.One(&p)
	str,err:= json.Marshal(p)
	if err != nil{
		log.Error("products解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Document="production_group"
	query,err = model.DB().QueryOne(bson.M{"productid":id})
	if err != nil{
		log.Error("production_group查询出错",err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	p1:= productiongroup{}
	query.One(&p1)
	str1,err1 := json.Marshal(p1)
	if err1 != nil{
		log.Error("production_group解析参数错误",err1)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err1
	}
	model.DB().Document="design_group"
	query,err = model.DB().QueryOne(bson.M{"productid":id})
	if err != nil{
		log.Error("design_group查询出错",err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	p2:= designgroup{}
	query.One(&p2)
	str2,err2 := json.Marshal(p2)
	if err2 != nil{
		log.Error("production_group解析参数错误",err2)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err2
	}
	model.DB().Document="editor_group"
	query,err = model.DB().QueryOne(bson.M{"productid":id})
	if err != nil{
		log.Error("editor_group查询出错",err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	p3:= editorgroup{}
	query.One(&p3)
	str3,err3 := json.Marshal(p3)
	if err2 != nil{
		log.Error("editor_group解析参数错误",err3)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err3
	}
	model.DB().Document="face_group"
	query,err = model.DB().QueryOne(bson.M{"productid":id})
	if err != nil{
		log.Error("face_group查询出错",err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	p4:= facegroup{}
	query.One(&p4)
	str4,err4 := json.Marshal(p4)
	if err4 != nil{
		log.Error("face_group解析参数错误",err4)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err4
	}
	model.DB().Document="operation_group"
	query,err = model.DB().QueryOne(bson.M{"productid":id})
	if err != nil{
		log.Error("operation_group查询出错",err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	p5:= operationgroup{}
	query.One(&p5)
	str5,err5 := json.Marshal(p5)
	if err5 != nil{
		log.Error("operation_group解析参数错误",err5)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err5
	}
	model.DB().Document="photography_group"
	query,err = model.DB().QueryOne(bson.M{"productid":id})
	if err != nil{
		log.Error("photography_group查询出错",err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	p6:= photographygroup{}
	query.One(&p6)
	str6,err6 := json.Marshal(p6)
	if err6 != nil{
		log.Error("photography_group解析参数错误",err6)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err6
	}
	model.DB().Document="sku"
	query,_,err = model.DB().Query(bson.M{"productid":id}, 0, 0)
	if err != nil{
		log.Error("sku查询出错",err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	p7:= []sku{}
	query.All(&p7)
	str7,err7 := json.Marshal(p7)
	if err7 != nil{
		log.Error("sku解析参数错误",err7)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err7
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
		"product":string(str),
		"production_group":string(str1),
		"designgroup":string(str2),
		"editorgroup":string(str3),
		"facegroup":string(str4),
		"operationgroup":string(str5),
		"photographygroup":string(str6),
		"sku":string(str7),
	})

	return nil
}
func DelBomHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelBomHandler",id)
	model.DB().Dbname="OxyCms"
	model.DB().Document="bom"
	err:=model.DB().Remove(bson.M{"_id":bson.ObjectIdHex(id)})
	if err != nil{
		log.Error("删除出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":  "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
	})

	return nil
}